// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   PropertyEditorPanel.java

package jones.gui;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.lang.reflect.Method;
import javax.swing.*;

// Referenced classes of package jones.gui:
//            CannotEditPropertyException

public class PropertyEditorPanel extends JPanel
{

    private static final String BUTTON = "Button";
    private static final String EDITOR = "Editor";
    private PropertyDescriptor propertyDescriptor = null;
    private PropertyEditor propertyEditor = null;
    private Object beanObject = null;
    private CardLayout cardLayout = null;
    private JButton propertyButton = null;
    private JComboBox comboBox = null;
    private JTextField textField = null;

    public PropertyEditorPanel(PropertyDescriptor descriptor, Object bean)
        throws CannotEditPropertyException
    {
        cardLayout = new CardLayout();
        if (descriptor == null || bean == null)
            throw new NullPointerException("Arguments cannot be null (descriptor).");
        propertyDescriptor = descriptor;
        beanObject = bean;
        if (propertyDescriptor.getWriteMethod() == null)
            throw new CannotEditPropertyException("No write method.");
        if (propertyDescriptor.isHidden())
            throw new CannotEditPropertyException("Property is hidden.");
        if (propertyDescriptor.isExpert())
            throw new CannotEditPropertyException("Property is expert.");
        if (propertyDescriptor instanceof IndexedPropertyDescriptor)
            throw new CannotEditPropertyException("Property is indexed.");
        Class propertyEditorClass = propertyDescriptor.getPropertyEditorClass();
        if (propertyEditorClass != null)
            try
            {
                propertyEditor = (PropertyEditor)propertyEditorClass.newInstance();
            }
            catch (InstantiationException ex)
            {
                propertyEditor = PropertyEditorManager.findEditor(propertyDescriptor.getPropertyType());
            }
            catch (IllegalAccessException ex)
            {
                propertyEditor = PropertyEditorManager.findEditor(propertyDescriptor.getPropertyType());
            }
        else
            propertyEditor = PropertyEditorManager.findEditor(propertyDescriptor.getPropertyType());
        if (propertyEditor == null)
            throw new CannotEditPropertyException("No property editor found.");
        try
        {
            propertyEditor.setValue(propertyDescriptor.getReadMethod().invoke(beanObject, new Object[0]));
        }
        catch (Exception ex)
        {
            throw new CannotEditPropertyException("Could not initialize property editor.");
        }
        setLayout(((java.awt.LayoutManager) (cardLayout)));
        if (propertyEditor.isPaintable())
        {
            propertyButton = ((JButton) (new JButton() {

                private final int BORDER = 5;

                public void paintComponent(Graphics g)
                {
                    super.paintComponent(g);
                    Rectangle r = new Rectangle(getSize());
                    r.setRect(r.x + 5, r.y + 5, r.width - 10, r.height - 10);
                    propertyEditor.paintValue(g, r);
                }

            }
));
            propertyEditor.addPropertyChangeListener(new PropertyChangeListener() {

                public void propertyChange(PropertyChangeEvent e)
                {
                    propertyButton.repaint();
                }

            }
);
        } else
        {
            if (propertyEditor.getValue() == null)
                propertyButton = new JButton("");
            else
                propertyButton = new JButton(propertyEditor.getAsText());
            propertyEditor.addPropertyChangeListener(new PropertyChangeListener() {

                public void propertyChange(PropertyChangeEvent e)
                {
                    if (propertyEditor.getValue() == null)
                        propertyButton.setText("");
                    else
                        propertyButton.setText(propertyEditor.getAsText());
                    propertyButton.repaint();
                }

            }
);
        }
        propertyButton.setHorizontalAlignment(2);
        add(((java.awt.Component) (propertyButton)), "Button");
        if (propertyEditor.supportsCustomEditor())
        {
            propertyButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    JDialog dialog = new JDialog();
                    dialog.setModal(true);
                    dialog.getContentPane().add(propertyEditor.getCustomEditor(), "Center");
                    dialog.setDefaultCloseOperation(2);
                    dialog.pack();
                    dialog.show();
                }

            }
);
        } else
        {
            propertyButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    cardLayout.show(((Container) (PropertyEditorPanel.this)), "Editor");
                }

            }
);
            if (propertyEditor.getTags() != null)
            {
                String tags[] = propertyEditor.getTags();
                comboBox = new JComboBox(((Object []) (tags)));
                if (propertyEditor.getValue() != null)
                    comboBox.setSelectedItem(((Object) (propertyEditor.getAsText())));
                comboBox.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e)
                    {
                        propertyEditor.setAsText((String)comboBox.getSelectedItem());
                        cardLayout.show(((Container) (PropertyEditorPanel.this)), "Button");
                    }

                }
);
                add(((java.awt.Component) (comboBox)), "Editor");
            } else
            {
                textField = new JTextField(propertyEditor.getAsText());
                textField.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e)
                    {
                        try
                        {
                            propertyEditor.setAsText(textField.getText());
                            cardLayout.show(((Container) (PropertyEditorPanel.this)), "Button");
                        }
                        catch (Exception ex)
                        {
                            JOptionPane.showMessageDialog(((java.awt.Component) (textField)), ((Object) (ex)), "Invalid Property Value", 0);
                        }
                    }

                }
);
                textField.addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

                    public void mouseClicked(MouseEvent e)
                    {
                        if (e.getClickCount() == 2)
                            textField.selectAll();
                    }

                }
)));
                add(((java.awt.Component) (textField)), "Editor");
            }
        }
        propertyEditor.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent e)
            {
                Object args[] = {
                    propertyEditor.getValue()
                };
                try
                {
                    propertyDescriptor.getWriteMethod().invoke(beanObject, args);
                }
                catch (Exception ex)
                {
                    JOptionPane.showMessageDialog(((java.awt.Component) (PropertyEditorPanel.this)), ((Object) (ex)), "Property Change Failed", 0);
                }
            }

        }
);
    }







}
